<?php
class AdminHome
{	
	public $db;
	public $table;
	public $menutable;
	private $pmenu = array();
	private $userMenuId;
	private $userMenuArr;
	private $usergroup;
	public $allMenu=array();
	
	private $menuHtml;
	public function __construct()
	{
		$this->db=new Mysql();
		$this->table=$this->db->pre.'home';
		$this->menutable=$this->db->pre.'menu';
		$user = g('user');	
		//print_r($user);	
		foreach ($user['userinfo']['role'] as $k=>$v){
			$this->userMenuArr[] = $v['menuid'];
		}		 
		
		$sql = "SELECT * FROM ".$this->menutable." WHERE `isuse` = 1 ORDER BY `sort` ASC ";		
		$this->pmenu = $this->db->getAllCached($sql);
		$arr = array();
		if ($user['userinfo']['isadmin']>=2)//非超级管理员
		{
			foreach ($this->pmenu as $k=>$v){
				if (in_array($v['id'], $this->userMenuArr) ){
					
					$arr[] = $v;
				}
			}
//			/print_r($arr);
			$this->pmenu = $arr;
		}
		
		$sql = "SELECT * FROM ".$this->db->pre."user_group ";
		$this->usergroup = $this->db->getAll($sql);	

		$sql = "SELECT * FROM ".$this->menutable." WHERE  1 ORDER BY pmodule,module,`sort`,id ASC ";		
		$this->allMenu = $this->db->getAll($sql);
	}
	public function getAllMenu(){
	   
	    return $this->allMenu;
	}
	public function getMenuById($id){
	    foreach ($this->allMenu as $k=>$v){
	        if ($v['id']==$id){
	            return $v;
	        }
	    }
	    return false;
	}
	public function userMenuHaveCid($id)
	{
		
		foreach ($this->userMenuArr as $k=>$v){
			foreach ($this->pmenu as $key=>$value){
				if ($value['id']==$v){
					$arr = $value;break;
				}
			}
			if ($arr['pid']==$id) return true;
		}
		return false;
	}
	public function getThisMenu()
	{
		
		$html='<ul>';
		
		foreach ($this->pmenu as $k=>$v)
		{			
			// and $v['pid']!=0 and $v['isshow']==1
			if ($v['isshow']==1 and $_GET['module']==$v['module']){
				if ($v['pmodule']== $_GET['pmodule'] and $_GET['module']==$v['module'] and $_GET['actions']==$v['action']){	
					$pstyle = 'style="color:#ff6600;font-weight:bold;"';
					$listye = 'style="background:#bbdde5;"';
				}else{
					$pstyle = 'style="color:#000;"';
					$listye = 'style="background:#bbdde5;"';
				}
					if ($v['pid']==0){				
						$html .= '<li '.$listye.'><a '.$pstyle
						.' href="'.MVC_WWW_HOST.'admin.php/'.$v['pmodule'].'/'.$v['module'].'/'.$v['action'].'/'.$v['parm'].'" >'.$v['name'].'</a> </li>';
						$cmenu = $this->getChildMenu($this->pmenu, $v['id']);
						foreach ($cmenu as $ke=>$va){
							if ($va['pmodule']== $_GET['pmodule'] and $_GET['module']==$va['module'] and $_GET['actions']==$va['action']){	
								$pstyle = 'style="color:#ff6600;font-weight:bold;background:#BBDDE5;"';
							}else{
								$pstyle = 'style="color:#000;"';
							}
							$html .= '<li ><a '.$pstyle.' href="'
							.MVC_WWW_HOST.'admin.php/'.$va['pmodule'].'/'.$va['module'].'/'.$va['action'].'/'.$va['parm'].'" >'.$va['name'].'</a> </li>';
					
						}
					}				
				
			}
		}
		return $html.'</ul>';
	}
	public function getThisMenuId()
	{
		//print_r($this->pmenu );die();
		if ($_GET['module']=='api') return true;
		foreach ($this->pmenu as $k=>$v)
		{			
			if ($v['pmodule']== $_GET['pmodule'] and $_GET['module']==$v['module'] and $_GET['actions']==$v['action']){				
				return $v['id'];				
			}			
		}
		return false;
	}
	public function getThisNav(){
		$html='';
		$sysArray = array('pmodule','module','actions');
		$keys = '';
		foreach ($_GET as $ks=>$vs){
			if (@$_GET['page']!='') continue;
			if (!in_array($ks, $sysArray) and !empty($ks)){
				
				$keys .=$ks.'/'.$vs;
			}
		}
		
		foreach ($this->pmenu as $k=>$v)
		{
			if (empty($v['parm'])) $v['parm'] = $keys;
			if ($v['pmodule']== $_GET['pmodule'] and $_GET['module']==$v['module'] and $_GET['actions'] == $v['action'] and $keys==$v['parm']){
				
					
				$html = '<a href="'.MVC_WWW_HOST.'admin.php/'.$v['pmodule'].'/'.$v['module'].'/'.$v['action'].'/'.$v['parm'].'" >'.$v['name'].'</a>';
				if ($v['pid']!=0){
					$p = $this->getNavPName($v['pid']);
					$html = '<a href="'.MVC_WWW_HOST.'admin.php/'.$p['pmodule'].'/'.$p['module'].'/'.$p['action'].'/'.$p['parm'].'" >'.$p['name'].'</a> > '.$html;
				}
					
				
			}
		}		
	
		$res = '<a href="'.MVC_WWW_HOST.'admin.php/'.$v['pmodule'].'/home/list">后 台</a> > '.$html;
		if (Http::get('module')!='home')
		{
			$res =$res.'&nbsp;&nbsp;&nbsp; <a href="javascript:window.history.back()">[返回上一页]</a>';
		}
		return $res;
	}
	public function getNavPName($id){
		foreach ($this->pmenu as $k=>$v){
			if ($v['id']==$id){
				return $v;
			}
		}
	}
	public function getMenu()
	{
		
		$html = '';
		foreach ($this->pmenu as $key=>$value)
		{
			if ($value['pid']==0 and $value['isshow']==1){
				$child = $this->getChildMenu($this->pmenu, $value['id']);
				if (count($child)>0){
					$c = ' onmouseover="showChild('."'".$value['module']."'".')" onmouseout="hideChild('."'".$value['module']."'".');"';
					$c1 = '<div id="'.$value['module'].'" class="menu_li_c" ><ul >';
					foreach ($child as $k=>$v){
						$c1 .= '<li><a href="'.MVC_WWW_HOST.'admin.php/'.$v['pmodule'].'/'.$v['module'].'/'.$v['action'].'/'.$v['parm'].'" target="'.$v['target'].'">'.$v['name'].'</a></li>';
					}
					$c1 .= '</ul></div>';
				}else{
					$c ='';
					$c1 = '';
				}
				$html .= '<div class="menu_li" '.$c.'><a href="'.MVC_WWW_HOST.'admin.php/'.$value['pmodule'].'/'.$value['module'].'/'.$value['action'].'/'.$value['parm'].'" target="'.$value['target'].'">'.$value['name'].'</a>'.$c1.'</div>';
			}
		}
		return $html;
		
	}
	public function getChildMenu($rs,$pid){
		$res = array();
		foreach ($rs as $k=>$v)
		{
			if ($v['pid']==$pid and $v['isshow']==1){
				$res[] = $v;
			}
		}
		return $res;
	}
	/**
	 * 
	 * 获取用户列表
	 * @param unknown_type $uid
	 */
	public function getMenuList($uid)
	{
		$userinfo = $this->getUserinfo($uid);
		$plantformId = $userinfo['plantform_id'];
		foreach ($this->usergroup as $k=>$v){
			if ($plantformId==$v['plantform_id']){
				$arr[] = $v['menuid'];
			}	
		}
		$sql = "SELECT a.`menuid`,b.`name`,b.`pid` FROM ".$this->db->pre."user_role a LEFT JOIN ".$this->db->pre."menu b ON a.menuid = b.id WHERE a.uid = '$uid' ";
		$userMenu = $this->db->getAll($sql);
		$menuArr = array();
		foreach ($userMenu as $value) {
			$menuArr[]=$value['menuid'];
		}
		foreach ($this->pmenu as $key=>$value) {
			if ($value['pid']==0){
				$checked='';
				
				if (in_array($value['id'], $menuArr)){
					$checked = 'checked="checked"';
				}else{
					if ($this->menuHaveChild($menuArr,$value['id'])){
							$checked = 'checked="checked"';	
					}
				}
				$this->menuHtml.='<tr><td><input type="checkbox" class="checkitem" value="'.$value['id'].'" '.$checked.'> '.$value['name'].'</td></tr>';				
				$this->getMenuChildTr($value['id'],'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',$menuArr);
			}
		}
		return $this->menuHtml;
		
	}
	public function getMenuChildTr($pid,$dist,$menuArr){
		foreach ($this->pmenu as $key=>$value)
		{
			if ($value['pid']==$pid)
			{				
				$checked='';
				
				if (in_array($value['id'], $menuArr)){
					$checked = 'checked="checked"';
				}else{
					if ($this->menuHaveChild($menuArr,$value['id'])){
							$checked = 'checked="checked"';	
					}
				}
				$this->menuHtml.='<tr><td>'.$dist.'<input type="checkbox" class="checkitem" value="'.$value['id'].'" '.$checked.'> '.$value['name'].'</td></tr>';				
				$this->getMenuChildTr($value['id'],$dist.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',$menuArr);
			}
		}
	}
	public function menuHaveChild($menuArr,$pid)
	{
		foreach ($menuArr as $key=>$value) {
			$va=array();
			foreach ($this->pmenu as $k=>$v){
				
				if ($value == $v['id']){
					$va = $v;
					break;
				}
			}
			if ($va){
				if ($va['pid']==$pid) return true;
			}
		}
		return false;
	}
	public function getUserinfo($uid){
		$sql = "SELECT  *  FROM ".$this->db->pre."user   WHERE uid = $uid";
		return $this->db->getRow($sql);
	}
	
	/**
	 * 
	 * 保存权限
	 * @param unknown_type $uid
	 * @param unknown_type $role
	 */
	public function saveRole($uid,$menuids)
	{
		//print_r($menuids);
		$sql = "DELETE FROM ".$this->db->pre."user_role WHERE uid = '$uid' ";
		//echo $sql;
		$this->db->delete($sql);
		$arr = explode(',', $menuids);
		foreach ($arr as $k=>$v){
			if ($v){
				$data = array('uid'=>$uid,'menuid'=>$v);
				$this->db->insert($this->db->pre."user_role", $data);
			}
		}
		return true;
	}
	/**
	 * 
	 * 添加用户
	 * @param unknown_type $data
	 */
	public function addUser($data)
	{
		return $this->db->insert($this->db->pre."user", $data,true);
	}
	public function updateUser($data,$uid)
	{
		return $this->db->updateData($this->db->pre."user", $data, " WHERE uid = '$uid'");
	}
	/**
	 * 
	 * 添加用户菜单权限
	 * @param unknown_type $thisuser
	 * @param unknown_type $uid
	 */
	public function addRoleMenu($uid,$plantformId=null)
	{
		$arr = array();
		
		foreach ($this->usergroup as $k=>$v){
			if ($plantformId && $plantformId==$v['plantform_id']){
				$arr[] = $v['menuid'];
			}else{
				$arr[] = $v['menuid'];
			}
			
		}
	
		foreach ($this->pmenu  as $k=>$v){
			if (in_array($v['id'],$arr) or in_array($v['pid'], $arr))
			{
				$data = array('uid'=>$uid,'menuid'=>$v['id']);
				$this->db->insert($this->db->pre."user_role", $data);
			}
		}
		return true;
	}

	/**
	 * 
	 * 删除用户
	 * @param unknown_type $uid
	 */
	public function delUser($uid)
	{
		$sql = "DELETE FROM ".$this->db->pre."user_role WHERE uid in ($uid)";
		$this->db->delete($sql);
		$sql = "DELETE FROM ".$this->db->pre."user WHERE uid in($uid)";
		return $this->db->delete($sql);
	}
	
	public function getPlantform()
	{
		$sql = "SELECT id,ptoename,ptocname FROM sells_plantform ";
		return $this->db->getAll($sql);
	}
	
	public function chartLineData($data,$dotcolor,$linecolor)
	{
		$dataview =array ( 'type' => 'line', 	
					 	   'dot-style' => array ( 'type' => 'solid-dot', 'dot-size' => 3, 'halo-size' => 1, 'colour' => $dotcolor, ), 'width' => 2, 'colour' =>$linecolor, );
					 		;
		$dataview['values']=$data;
		return $dataview;
	}
	public function chartLine($title,$data,$ymax,$xmin,$xmax)
	{
		
		$arr = array ('title' => array ( 'text' => $title, ), 
					'elements'=>$data,
					'y_axis' => array ( 'min' => 0, 'max' => $ymax, 'steps' => 1, ), 
					'x_axis'=>array('min'=>$xmin,'max'=>$xmax, 'steps'=>86400,'text'=>'#date:MY#','labels'=>array('text'=>'#date:m-j#','steps'=>86400,'visible-steps'=>1,'rotate'=>1))
			
		);
		return $arr;
	}
	
}